import {
    baseUrl
} from './config'
import axios from 'axios';
import store from '../store'


axios.defaults.baseURL = baseUrl; //接口api请求地址

//设置请求超时
axios.defaults.timeout = 10000;

//设置请求头
axios.defaults.headers.post['Content-Type'] = 'application/json;charset=UTF-8';


/**
 * get方法，对应get请求
 * @param {String} url [请求的url地址]
 * @param {Object} params [请求时携带的参数]
 */
export function get(url, params) {
    return new Promise((resolve, reject) => {
        axios.defaults.headers.token = store.state.user.gearToken
        axios.get(url, {
            params: params
        }).then(res => {
            resolve(res.data);
        }).catch(err => {
            reject(err.data)
        })
    });
}

/**
 * post方法，对应post请求
 * @param {String} url [请求的url地址]
 * @param {Object} params [请求时携带的参数]
 * QS.stringify(params)
 */
export function post(url, params) {
    return new Promise((resolve, reject) => {
        axios.defaults.headers.token = store.state.user.gearToken
        axios.post(url, params)
            .then(res => {
                resolve(res.data);
                // if (res.data.info == 'token过时' || res.data.info == '缺失token') {
                //     alert('请重新登录系统！')
                //     window.location.href = "/xQsyaldw"
                // }
            })
            .catch(err => {
                reject(err.data)
            })
    });
}